<?php
class model_TagMapper {
    private $table = 'tags';
    private $toNewsTable = 'tagtonews';

    public function  __construct() {
        model_ConfigMapper::connect();
    }

    public function find($id) { //находим сообщение, записываем его содержание в свойства объекта
        $query = 'SELECT * from '. $this->table .' WHERE id = '. $id;
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);

        if ($row) {
            $m = new model_TagModel($row);
        } else {
            $m = new model_TagModel(null);
        }

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        return $m;
    }

    public function fetchAll() { //
        $query = 'SELECT * from '. $this->table .' ORDER BY `id` ASC';
        $result = mysql_query($query);

        $entries = array();

        while ($row = mysql_fetch_array($result)) {
            $entry = new model_TagModel($row);
            $entries[] = $entry;
        }

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        return $entries;
    }

    public function fetchById($id) { //
        $query = 'SELECT tagid from '. $this->toNewsTable .' WHERE newsid = '.$id.' ORDER BY `id` ASC';
        $result = mysql_query($query);
        $entries = array();

        while ($row = mysql_fetch_row($result)) {

            $entry = $this->find($row[0]);
            $entries[] = $entry;
        }

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        return $entries;
    }

    public function save($model) {
        if (null == $model->getId()) {
            //сохраняем новую
            $query = 'INSERT INTO '. $this->table .' (text) VALUES ("'. $model->getText() .'")';
            $result = mysql_query($query);
        } else {
            //заменяем строку строкой
            $query = 'UPDATE '. $this->table .' SET text="'. $model->getText() .'" WHERE id='. $model->getId();
            $result = mysql_query($query);
        }

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();
    }

    public function delete($id) {// собственно удаляем
        $query = 'DELETE FROM '. $this->table .' WHERE id='.$id;
        $result = mysql_query($query);

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        $query = 'DELETE FROM task4base.'. $this->toNewsTable .' WHERE tagid='.$id;
        $result = mysql_query($query);

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

    }

    public function saveTagsToNews($id, $tag) {
        $query = 'DELETE FROM '. $this->toNewsTable .' WHERE newsid='.$id; //удаляем старые теги
        $result = mysql_query($query);
        foreach ($tag as $val) {
            $query = 'INSERT INTO '. $this->toNewsTable .' (newsid, tagid) VALUES ("'. $id .'","'. $val .'")'; //записываем новые
            $result = mysql_query($query);

            $timer = model_ConfigMapper::instance();
            $timer->setSqlCounter();
        }

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();
    }
}